package com.jokerpan.project.controller;

import com.jokerpan.project.annotation.AccessLimit;
import com.jokerpan.project.commons.ListResult;
import com.jokerpan.project.commons.ResultBuilder;
import com.jokerpan.project.service.AliyunOssService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;

/**
 * @author jokerpan
 */
@RestController
@RequestMapping("oss")
@Api("Aliyun OSS 文件上传Api")
public class AliyunOssController {

    @Resource
    AliyunOssService aliyunOssService;

    /**
     * 公共附件上传接口
     * @param table 关联表名
     * @param id 关联id
     * @param file
     * @return
     */
    @RequestMapping("/upload")
    @AccessLimit(seconds = 6, maxCount = 5)
    @PreAuthorize("hasPermission('','user:update')")
    @ApiOperation("文件上传")
    public ListResult<Object> upload(@RequestParam("table") String table, @RequestParam("id") Integer id, @RequestParam("file")MultipartFile file){
        String result = aliyunOssService.uploadFile(table, id, file);
        if (!result.equals("error")){
            return ResultBuilder.buildListSuccess(result);
        }else {
            return ResultBuilder.buildListFail("上传失败!");
        }
    }

    /**
     * 临时附件上传接口
     * @param file
     * @param id 用户主键
     * @return
     */
    @RequestMapping("/uploadTemporary")
    @AccessLimit(seconds = 6, maxCount = 5)
    @PreAuthorize("hasPermission('','user:update')")
    @ApiOperation("临时文件上传")
    public ListResult<Object> uploadTemporary(@RequestParam("id") Integer id, @RequestParam("file")MultipartFile file){
        String result = aliyunOssService.uploadTemporaryFile(id, file);
        if (!result.equals("error")){
            return ResultBuilder.buildListSuccess(result);
        }else {
            return ResultBuilder.buildListFail("上传失败!");
        }
    }
}
